package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"math/rand"
	"os"
	"time"
)

const IDHead string = "identity_"

var Dats []DA
var Count = 0

func init() {
	rand.Seed(time.Now().UnixNano())
}

func solve(sli []DA) ([]DA, error) {
	len := len(sli)
	if len <= 1 {
		return []DA{}, errors.New("参数不合法")
	}

	for i := len - 1; i > 0; i-- {
		randNum := rand.Intn(i)
		sli[i], sli[randNum] = sli[randNum], sli[i]
	}
	return sli, nil
}
func main() {
	str := "`This is a backtick inside a string.`"
	fmt.Println(str)
	GetMoveUp()
	GetMoveDown()
	GetMoveW()
	GetMoveS()
	GetMoveRight()
	GetMoveLeft()
	GetMoveTakeOff()
	GetMoveland()
	GetMoveGotZero()
	GetMoveRateLeft()
	GetMoveRateRight()
	Dats, err := solve(Dats) //打散训练数组
	if err != nil {
		panic(err)
	}
	for i := 0; i < len(Dats); i++ { //对散训练数组，重新编码 Id
		Dats[i].Id = IDHead + fmt.Sprint(i)
	}
	fmt.Println(len(Dats))
	bu, err := json.Marshal(Dats)
	if err != nil {
		panic(err)
	}
	os.WriteFile("qwen.json", bu, os.ModePerm)
}
func GetMoveUp() {
	for i := 0; i < 100; i += 1 {
		var Dat DA
		var CS []conversation
		var c conversation
		c.From = "user"
		if i%2 == 0 {
			c.Value = "向上移动" + fmt.Sprint(i) + "个单位"
		} else {
			c.Value = "上移" + fmt.Sprint(i)
		}

		CS = append(CS, c)
		var c2 conversation
		c2.From = "assistant"
		c2.Value = "```python aw.fly_to([aw.get_drone_position()[0], aw.get_drone_position()[1], aw.get_drone_position()[2]-" + fmt.Sprint(i) + "])```"
		CS = append(CS, c2)
		Dat.Id = IDHead + fmt.Sprint(Count)
		Dat.Conversations = CS
		Dats = append(Dats, Dat)
		Count++
	}
}
func GetMoveDown() {
	for i := 0; i < 100; i += 1 {
		var Dat DA
		var CS []conversation
		var c conversation
		c.From = "user"
		if i%2 == 0 {
			c.Value = "向下移动" + fmt.Sprint(i) + "个单位"
		} else {
			c.Value = "下移" + fmt.Sprint(i)
		}
		CS = append(CS, c)
		var c2 conversation
		c2.From = "assistant"
		c2.Value = "```python aw.fly_to([aw.get_drone_position()[0], aw.get_drone_position()[1], aw.get_drone_position()[2]+" + fmt.Sprint(i) + "])```"
		CS = append(CS, c2)
		Dat.Id = IDHead + fmt.Sprint(Count)
		Dat.Conversations = CS
		Dats = append(Dats, Dat)
		Count++
	}
}
func GetMoveW() {
	for i := 0; i < 100; i += 1 {
		var Dat DA
		var CS []conversation
		var c conversation
		c.From = "user"

		if i%2 == 0 {
			c.Value = "向前移动" + fmt.Sprint(i) + "个单位"
		} else {
			c.Value = "前移" + fmt.Sprint(i)
		}
		CS = append(CS, c)
		var c2 conversation
		c2.From = "assistant"
		c2.Value = "```python aw.fly_to([aw.get_drone_position()[0]+" + fmt.Sprint(i) + ", aw.get_drone_position()[1], aw.get_drone_position()[2]])```"
		CS = append(CS, c2)
		Dat.Id = IDHead + fmt.Sprint(Count)
		Dat.Conversations = CS
		Dats = append(Dats, Dat)
		Count++
	}
}
func GetMoveS() {
	for i := 0; i < 100; i += 1 {
		var Dat DA
		var CS []conversation
		var c conversation
		c.From = "user"
		if i%2 == 0 {
			c.Value = "向后移动" + fmt.Sprint(i) + "个单位"
		} else {
			c.Value = "后移" + fmt.Sprint(i)
		}
		CS = append(CS, c)
		var c2 conversation
		c2.From = "assistant"
		c2.Value = "```python aw.fly_to([aw.get_drone_position()[0]-" + fmt.Sprint(i) + ", aw.get_drone_position()[1], aw.get_drone_position()[2]])```"
		CS = append(CS, c2)
		Dat.Id = IDHead + fmt.Sprint(Count)
		Dat.Conversations = CS
		Dats = append(Dats, Dat)
		Count++
	}
}
func GetMoveRight() {
	for i := 0; i < 100; i += 1 {
		var Dat DA
		var CS []conversation
		var c conversation
		c.From = "user"

		if i%2 == 0 {
			c.Value = "向右移动" + fmt.Sprint(i) + "个单位"
		} else {
			c.Value = "右移" + fmt.Sprint(i)
		}
		CS = append(CS, c)
		var c2 conversation
		c2.From = "assistant"
		c2.Value = "```python aw.fly_to([aw.get_drone_position()[0], aw.get_drone_position()[1]+" + fmt.Sprint(i) + ", aw.get_drone_position()[2]])```"
		CS = append(CS, c2)
		Dat.Id = IDHead + fmt.Sprint(Count)
		Dat.Conversations = CS
		Dats = append(Dats, Dat)
		Count++
	}
}
func GetMoveLeft() {
	for i := 0; i < 100; i += 1 {
		var Dat DA
		var CS []conversation
		var c conversation
		c.From = "user"
		if i%2 == 0 {
			c.Value = "向左移动" + fmt.Sprint(i) + "个单位"
		} else {
			c.Value = "左移" + fmt.Sprint(i)
		}
		CS = append(CS, c)
		var c2 conversation
		c2.From = "assistant"
		c2.Value = "```python aw.fly_to([aw.get_drone_position()[0], aw.get_drone_position()[1]-" + fmt.Sprint(i) + ", aw.get_drone_position()[2]])```"
		CS = append(CS, c2)
		Dat.Id = IDHead + fmt.Sprint(Count)
		Dat.Conversations = CS
		Dats = append(Dats, Dat)
		Count++
	}
}
func GetMoveTakeOff() {
	var vls []string
	vls = append(vls, "无人机起飞")
	vls = append(vls, "无人机起行")
	vls = append(vls, "无人机起来")
	vls = append(vls, "起飞无人机")
	vls = append(vls, "无人机启动")
	for i := 0; i < len(vls); i++ {
		var Dat DA
		var CS []conversation
		var c conversation
		c.From = "user"
		c.Value = vls[i]
		CS = append(CS, c)
		var c2 conversation
		c2.From = "assistant"
		c2.Value = "```python aw.takeoff()```"
		CS = append(CS, c2)
		Dat.Id = IDHead + fmt.Sprint(Count)
		Dat.Conversations = CS
		Dats = append(Dats, Dat)
		Count++
	}

}
func GetMoveland() {

	var vls []string
	vls = append(vls, "无人机降落")
	vls = append(vls, "无人机着陆")
	vls = append(vls, "无人机不飞了")
	vls = append(vls, "降落无人机")
	vls = append(vls, "无人机停止飞行")
	vls = append(vls, "无人机别飞了")
	vls = append(vls, "着陆无人机")
	for i := 0; i < len(vls); i++ {
		var Dat DA
		var CS []conversation
		var c conversation
		c.From = "user"
		c.Value = vls[i]
		CS = append(CS, c)
		var c2 conversation
		c2.From = "assistant"
		c2.Value = "```python aw.land()```"
		CS = append(CS, c2)
		Dat.Id = IDHead + fmt.Sprint(Count)
		Dat.Conversations = CS
		Dats = append(Dats, Dat)
		Count++
	}

}
func GetMoveGotZero() {

	var vls []string
	vls = append(vls, "无人机回家")
	vls = append(vls, "无人机返回原点")
	vls = append(vls, "返回原点无人机")
	vls = append(vls, "无人机回到原点")
	vls = append(vls, "无人机返回初始位置")
	vls = append(vls, "返回原点")
	vls = append(vls, "回到原点")
	vls = append(vls, "飞往原点")
	vls = append(vls, "回到原点无人机")
	vls = append(vls, "飞往原点无人机")
	for i := 0; i < len(vls); i++ {
		var Dat DA
		var CS []conversation
		var c conversation
		c.From = "user"
		c.Value = vls[i]
		CS = append(CS, c)
		var c2 conversation
		c2.From = "assistant"
		c2.Value = "```python aw.fly_to([0, 0, 0])```"
		CS = append(CS, c2)
		Dat.Id = IDHead + fmt.Sprint(Count)
		Dat.Conversations = CS
		Dats = append(Dats, Dat)
		Count++
	}

}

// 偏航角度移动
func GetMoveRateRight() {
	for i := 0; i < 360; i += 5 {
		var Dat DA
		var CS []conversation
		var c conversation
		c.From = "user"
		V := float32(i) + rand.Float32()
		D := int(float32(i) + rand.Float32()*10)
		if i%2 == 0 {
			c.Value = "向右前方" + fmt.Sprintf("%.2f", V) + "度移动" + fmt.Sprint(D) + "个单位"
		} else {
			c.Value = "右前方" + fmt.Sprintf("%.2f", V) + "度移" + fmt.Sprint(D)
		}
		CS = append(CS, c)
		var c2 conversation
		c2.From = "assistant"
		c2.Value = "```python \nimport math\n\n#计算新的位置坐标，无人机向前" + fmt.Sprint(D) + "米，偏航角度" + fmt.Sprintf("%.2f", V) + "度，向右" + fmt.Sprint(D) + "米，偏航角度" + fmt.Sprintf("%.2f", V) + "度\ndelta_x=" + fmt.Sprint(D) + "*math.cos(math.radians(" + fmt.Sprintf("%.2f", V) + "))\ndelta_y=" + fmt.Sprint(D) + "*math.sin(math.radians(" + fmt.Sprintf("%.2f", V) + "))\n\nnew_x=aw.get_drone_position()[0]+delta_x\nnew_y=aw.get_drone_position()[1]+delta_y\nnew_z=aw.get_drone_position()[2]\n\n#飞往新的位置坐标\naw.fly_to([new_x,new_y,new_z])\n```"
		CS = append(CS, c2)
		Dat.Id = IDHead + fmt.Sprint(Count)
		Dat.Conversations = CS
		Dats = append(Dats, Dat)
		Count++
	}
}

// 偏航角度移动
func GetMoveRateLeft() {
	for i := 0; i < 360; i += 3 {
		var Dat DA
		var CS []conversation
		var c conversation
		c.From = "user"
		V := float32(i) + rand.Float32()
		D := int(float32(i) + rand.Float32()*10)
		if i%2 == 0 {
			c.Value = "向左前方" + fmt.Sprintf("%.2f", V) + "度移动" + fmt.Sprint(D) + "个单位"
		} else {
			c.Value = "左前方" + fmt.Sprintf("%.2f", V) + "度移" + fmt.Sprint(D)
		}
		CS = append(CS, c)
		var c2 conversation
		c2.From = "assistant"
		c2.Value = "```python \nimport math\n\n#计算新的位置坐标，无人机向前" + fmt.Sprint(D) + "米，偏航角度" + fmt.Sprintf("%.2f", V) + "度，向左" + fmt.Sprint(D) + "米，偏航角度" + fmt.Sprintf("%.2f", V) + "度\ndelta_x=" + fmt.Sprint(D) + "*math.cos(math.radians(" + fmt.Sprintf("%.2f", V) + "))\ndelta_y=" + fmt.Sprint(D) + "*math.sin(math.radians(" + fmt.Sprintf("%.2f", V) + "))\n\nnew_x=aw.get_drone_position()[0]+delta_x\nnew_y=aw.get_drone_position()[1]-delta_y\nnew_z=aw.get_drone_position()[2]\n\n#飞往新的位置坐标\naw.fly_to([new_x,new_y,new_z])\n```"
		CS = append(CS, c2)
		Dat.Id = IDHead + fmt.Sprint(Count)
		Dat.Conversations = CS
		Dats = append(Dats, Dat)
		Count++
	}
}

type DA struct {
	Id            string         `json:"id"`
	Conversations []conversation `json:"conversations"`
}
type conversation struct {
	From  string `json:"from"`
	Value string `json:"value"`
}
